/*
 * @Author: szx
 * @Date: 2022-02-16 13:29:21
 * @LastEditTime: 2022-02-16 13:35:32
 * @Description:
 * @FilePath: \leetcode\700-799\704\704_2.js
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// 左闭右闭
var search = function (nums, target) {
    let left = 0,
        right = nums.length - 1;
    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] > target) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return -1;
};

// 左闭右开
var search = function (nums, target) {
    let left = 0,
        right = nums.length;
    while (left < right) {
        const mid = Math.floor((left + right) / 2);
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] > target) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return -1;
};
// console.log(search([-1, 0, 3, 5, 9, 12], 9));
console.log(search([-1, 0, 3, 5, 9, 12], 2));
